package com.dx.login;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

public class LoginValidator  extends Validator {

	@Override
	protected void validate(Controller c) {
		String account = c.getPara("account");
		String password= c.getPara("password");
		System.out.println(account+":"+password);
		validateRequiredString("account", "accountMsg", "请输入登陆账号");
		validateRequiredString("password", "passwordMsg", "请输入密码");
		// 如果全部验证通过，则验证登陆账号和密码
		if (!this.getInvalid()) {
			User user=User.dao.findById(account);
			System.out.println("user:"+user);
			if ( user!= null) {
				if(!password.equals(user.get("password"))){
					addError("passwordMsg", "用户名密码不正确");
				}else {
					c.getSession().setAttribute("user", user);
				}
			}else {
				addError("accountMsg", "请输入正确登陆账号");
			}
		}
	}

	@Override
	protected void handleError(Controller c) {
		// TODO Auto-generated method stub
		c.keepPara();
		c.render("/WEB-INF/jsp/login/login.jsp");
		//c.redirect("/login");
	}

}
